<template>
  <div class="app-container">
    <!--操作按钮-->
    <div style="padding-bottom: 30px;" class="operation_container">
      <ul>
        <li>
          <el-button size="mini" @click="handleCancel">返回</el-button>
        </li>
        <li>
          <el-button size="mini" type="primary" @click="handleSubmit">保存</el-button>
        </li>
      </ul>
    </div>
    <!--折叠面板-->
    <el-collapse v-model="activeNames" @change="handleChange">
      <el-collapse-item name="basicInfo">
        <template slot="title">
          <span>基本信息</span>
          <i class="header-icon el-icon-info" />
        </template>
     <el-form ref="tbWmsMaterialReceiptForm" :model="tbWmsMaterialReceiptFormData" label-width="120px">
 <el-row :gutter="24" class="form-item-group">
           <!-- <el-col :span="6">
              <el-form-item label="收货单号：">
                <el-input v-model="tbWmsMaterialReceiptFormData.orderNo"  placeholder="" />
              </el-form-item>
            </el-col> -->

     <!--  <el-form-item label="仓库" prop="warehouseCode">
         <el-input v-model="tbWmsMaterialReceiptFormData.warehouseCode" placeholder="请输入" />
       </el-form-item> -->

            <el-col :span="6">
             <el-form-item label="客户代码：" prop="customerNo">
                <el-input v-model="tbWmsMaterialReceiptFormData.customerNo" class="filter-item">
                  <i slot="suffix" class="el-input__icon el-icon-search" @click="openFromSelector" />
                </el-input>
                <customer-selector :visible.sync="fromSelectorVisible" @selected="handleFromSelected" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
            <el-form-item label="客户名称" prop="customerName">
                <el-input v-model="tbWmsMaterialReceiptFormData.customerName" placeholder="请输入" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="订单日期：" prop="orderTime">
                <el-date-picker
                  v-model="tbWmsMaterialReceiptFormData.orderTime"
                  type="date"
                  placeholder="选择日期"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="预期收货日期：" prop="preArrivedTime">
                <el-date-picker
                  v-model="tbWmsMaterialReceiptFormData.preArrivedTime"
                  type="date"
                  placeholder="选择日期"
                />
              </el-form-item>
            </el-col>
         <!--  <el-col :span="6">
              <el-form-item label="状态：">
                <el-select v-model="tbWmsMaterialReceiptFormData.status" filterable clearable>
                  <el-option v-for="item in selects.paymentTypeList" :key="item.key" :label="item.label" :value="item.key" />
                </el-select>
              </el-form-item>
            </el-col> -->
             </el-row>
           <el-row :gutter="24" class="form-item-group">
                     <!-- <el-col :span="6">
                        <el-form-item label="销售(采购)单号：">

                        <el-input v-model="tbWmsMaterialReceiptFormData.sourceOrderNo" class="filter-item" >

                          <i slot="suffix" class="el-input__icon el-icon-search" @click="openContractSelector" />
                         <!--   <el-button style="float: right;" type="primary" icon="el-icon-plus" size="mini" @click="openContractSelector">选择合同</el-button>
                        -->
                         <!-- </el-input>
                           <sales-order-selector :visible.sync="contractSelectorVisible" @selected="handleSelectedContract" />


                         </el-form-item>

                      </el-col> -->
                     <!--  <el-col :span="6">
                         <el-form-item label="客户单号：">
                           <el-input v-model="tbWmsMaterialReceiptFormData.receiptNo"  placeholder="" />
                         </el-form-item>
                       </el-col> -->
                      
                    <!--  <el-col :span="6">
                         <el-form-item label="订单类型：">
                           <el-select v-model="tbWmsMaterialReceiptFormData.orderType" filterable clearable>
                             <el-option v-for="item in selects.orderTypeList" :key="item.key" :label="item.label" :value="item.key" />
                           </el-select>
                         </el-form-item>
                       </el-col> -->
                        </el-row>

                        <el-row :gutter="24" class="form-item-group">
                          <el-col :span="6">
                         <!-- <el-form-item label="发运地代码：" prop="warehouseCode">
                            <el-input v-model="tbWmsMaterialReceiptFormData.warehouseCode" class="filter-item">
                              <i slot="suffix" class="el-input__icon el-icon-search" @click="openWarehouseSelector" />
                            </el-input>
                            <warehouse-selector :visible.sync="warehousSelectorVisible" @selected="handleWarehouseSelected" />
                          </el-form-item> -->
                          <el-form-item label="发运地代码：" prop="warehouseCode">
                          <el-select v-model="tbWmsMaterialReceiptFormData.warehouseCode" filterable clearable   @change="customerAtSelectChange">
                            <el-option v-for="item in selects.warehouseList" :key="item.id" :label="item.warehouseName" :value="item.id" />
                          </el-select>
                          </el-form-item>
                          </el-col>
                                   <el-col :span="6">

                                      <el-form-item label="城市：" prop="departureIdList">
                                         <el-cascader
                                           v-model="tbWmsMaterialReceiptFormData.departureIdList"
                                           :options="selects.locationList"
                                           :props="{ expandTrigger: 'hover', label: 'locationName', value: 'id' }"
                                           @change="handleLocationChange"
                                         />
                                       </el-form-item>

                                   </el-col>
                                  <el-col :span="6">
                                  <el-form-item label="地址" prop="shipperAddress">
                                      <el-input v-model="tbWmsMaterialReceiptFormData.shipperAddress" placeholder="请输入" />
                                    </el-form-item>
                                  </el-col>
                                  <el-col :span="6">
                                    <el-form-item label="发货电话：">
                                      <el-input v-model="tbWmsMaterialReceiptFormData.shipperPhoneNo"  placeholder="" />
                                    </el-form-item>
                                  </el-col>


                                   </el-row>
                                  <el-row :gutter="24" class="form-item-group">
                                             <el-col :span="6">
                                               <el-form-item label="联系人：">
                                                 <el-input v-model="tbWmsMaterialReceiptFormData.shipperContact" disabled placeholder="系统自动生成" />
                                               </el-form-item>
                                             </el-col>
                                             <el-col :span="6">
                                              <el-form-item label="订单处理类型：">
                                                <el-select v-model="tbWmsMaterialReceiptFormData.orderHandlingType" filterable clearable>
                                                  <el-option v-for="item in selects.orderHandlingTypeList" :key="item.key" :label="item.label" :value="item.key" />
                                                </el-select>
                                              </el-form-item>
                                             </el-col>
                              <el-col :span="6">
                                   <el-form-item label="邮编：">
                                     <el-input v-model="tbWmsMaterialReceiptFormData.shipperZipNo"  placeholder="" />
                                   </el-form-item>
                                  </el-col>
                                <el-col :span="6">
                                   <el-form-item label="客户类型：">
                                     <el-select v-model="tbWmsMaterialReceiptFormData.customerType" filterable clearable>
                                       <el-option v-for="item in selects.customerTypeList" :key="item.key" :label="item.label" :value="item.key" />
                                     </el-select>
                                       </el-form-item>
                                  </el-col>
                                              </el-row>
                                             <!-- <el-row :gutter="24" class="form-item-group">
                                                         <el-col :span="6">
                                                           <el-form-item label="自定义1：">
                                                             <el-input v-model="tbWmsMaterialReceiptFormData.configColOne" disabled placeholder="系统自动生成" />
                                                           </el-form-item>
                                                         </el-col>
                                                         <el-col :span="6">
                                                          <el-form-item label="自定义2：">
                                                            <el-input v-model="tbWmsMaterialReceiptFormData.configColTwo" disabled placeholder="系统自动生成" />
                                                          </el-form-item>
                                                         </el-col>
                                                        <el-col :span="6">
                                                         <el-form-item label="自定义3：">
                                                           <el-input v-model="tbWmsMaterialReceiptFormData.configColThree" disabled placeholder="系统自动生成" />
                                                         </el-form-item>
                                                        </el-col>
                                                        <el-col :span="6">
                                                         <el-form-item label="自定义4：">
                                                           <el-input v-model="tbWmsMaterialReceiptFormData.configColFour" disabled placeholder="系统自动生成" />
                                                  </el-form-item>
                                                        </el-col>

                                                          </el-row> -->
                                                          <el-row :gutter="24" class="form-item-group">
                                                                     <el-col :span="6">
                                                                       <el-form-item label="自定义：">
                                                                         <el-input v-model="tbWmsMaterialReceiptFormData.configColOne"  placeholder="" />
                                                                       </el-form-item>
                                                                     </el-col>
                                                                     <el-col :span="18">
                                                                      <el-form-item label="备注：">
                                                                        <el-input v-model="tbWmsMaterialReceiptFormData.remark"  placeholder="" />
                                                                      </el-form-item>
                                                                     </el-col>

                                                                     </el-row>
      </el-form>

      </el-collapse-item>
      <el-collapse-item name="itemInfo">
        <template slot="title">
          <span>入库明细信息</span>
          <i class="header-icon el-icon-info" />
        </template>
   <el-button type="primary" style="margin-top: 6px; margin-bottom: 1px;" size="mini" @click="handleItemFormDisplay">填写明细</el-button>
   <el-button v-if="itemDisplay" type="primary" style="margin-top: 6px; margin-bottom: 1px;" size="mini" @click="handleItemAdd">添加</el-button>
   <el-card v-if="itemDisplay" class="box-card">
     <el-form ref="itemForm" :model="itemData" :rules="itemRules" :inline="true" size="mini" label-width="125px">
       <el-row :gutter="24" class="form-item-group">
         <el-col :span="8">
           <el-form-item label="结算依据：" prop="shipByPackOrqty">
             <el-select v-model="itemData.shipByPackOrqty" filterable clearable>
               <el-option v-for="item in selects.settledBasedOnList" :key="item.key" :label="item.label" :value="item.key" />
             </el-select>
           </el-form-item>
         </el-col>
        <!--结算依据-重量相关字段-->
        <el-col :span="8">
          <el-form-item label="货物名称：" prop="materialName">
            <el-input v-model="itemData.materialName" class="filter-item" >
              <i slot="suffix" class="el-input__icon el-icon-search" @click="openMaterialSelector" />
            </el-input>
            <warehouse-customer-selector ref="warehouseCustomerSelector" :visible.sync="materialSelectorVisible" :customer-no="tbWmsMaterialReceiptFormData.customerNo" @selected="handleMaterialSelected" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="货物单价：">
            <el-input v-model="itemData.unitPrice" type="number"  />
          </el-form-item>
        </el-col>
        <!--结算依据-重量相关字段--></el-row>
         <el-row :gutter="24" class="form-item-group">
        <el-col v-if="itemData.shipByPackOrqty === 'weight'" :span="8">
          <el-form-item label="重量单位：">
            <el-select v-model="itemData.weightMeasUnit" filterable clearable>
              <el-option v-for="item in selects.weightMeasUnitList" :key="item.key" :label="item.label" :value="item.key" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="itemData.shipByPackOrqty === 'weight'" :span="8">
          <el-form-item label="重量：">
            <el-input v-model="itemData.unitWeight" type="number" @input="handleCalShipmentExpense" />
          </el-form-item>
        </el-col>
        <el-col v-if="itemData.shipByPackOrqty === 'weight'" :span="8">
          <el-form-item label="总重：">
            <el-input v-model="itemData.weight" type="number" @input="handleCalShipmentExpense" />
          </el-form-item>
        </el-col>
        <!--结算依据-体积相关字段-->
        <el-col v-if="itemData.shipByPackOrqty === 'vol'" :span="8">
          <el-form-item label="单位体积：">
            <el-input v-model="itemData.unitVolume" type="number" @input="handleCalShipmentExpense" />
          </el-form-item>
        </el-col>
        <el-col v-if="itemData.shipByPackOrqty === 'vol'" :span="8">
          <el-form-item label="总体积：">
            <el-input v-model="itemData.volume" type="number" @input="handleCalShipmentExpense" />
          </el-form-item>
        </el-col>
        <el-col v-if="itemData.shipByPackOrqty === 'vol'" :span="8">
          <el-form-item label="长：">
            <el-input v-model="itemData.length" type="number" />
          </el-form-item>
        </el-col>
        <el-col v-if="itemData.shipByPackOrqty === 'vol'" :span="8">
          <el-form-item label="宽：">
            <el-input v-model="itemData.width" type="number" />
          </el-form-item>
        </el-col>
        <el-col v-if="itemData.shipByPackOrqty === 'vol'" :span="8">
          <el-form-item label="高：">
            <el-input v-model="itemData.high" type="number" />
          </el-form-item>
        </el-col>
        <!--结算依据-包装相关字段-->
        <el-col v-if="itemData.shipByPackOrqty === 'pack'" :span="8">
          <el-form-item label="包装单位：">
            <el-select v-model="itemData.packing" filterable clearable>
              <el-option v-for="item in selects.packingList" :key="item.key" :label="item.label" :value="item.key" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="itemData.shipByPackOrqty === 'pack'" :span="8">
          <el-form-item label="每包装数量：">
            <el-input v-model="itemData.qtyPerPack" type="number" disabled />
          </el-form-item>
        </el-col>
        <el-col v-if="itemData.shipByPackOrqty === 'pack'" :span="8">
          <el-form-item label="包装(件)：">
            <el-input v-model="itemData.packQty" type="number" @input="handleCalShipmentExpense" />
          </el-form-item>
        </el-col>
        <!--结算依据-数量相关字段-->

        </el-row>
        <el-row :gutter="24" class="form-item-group">
       <el-col  :span="8">
         <el-form-item label="数量：">
           <el-input v-model="itemData.quantity" type="number" @input="handleCalShipmentExpense" />
         </el-form-item>
       </el-col>
        <el-col :span="8">
          <el-form-item label="价值：">
            <el-input v-model="itemData.cargoValue" type="number" disabled />
          </el-form-item>
        </el-col>
       <el-col :span="8">
                 <el-form-item label="物料图号：">
                   <el-input v-model="itemData.skuCode"  />
                 </el-form-item>
              </el-col>


        </el-row>
         <el-row :gutter="24" class="form-item-group">
        <el-col :span="8">
         <el-form-item label="结算单价：">
           <el-input v-model="itemData.countPrice" type="number" @input="handleSettleExpense" />
         </el-form-item>
       </el-col>
       <el-col :span="8">
          <el-form-item label="结算数量：">
            <el-input v-model="itemData.countQuantity" type="number" @input="handleSettleExpense" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="仓储费总价：">
            <el-input v-model="itemData.countCarriage" type="number" />
          </el-form-item>
       </el-col>

       </el-row>

       </el-row>
     </el-form>
   </el-card>
   <el-divider />


        <el-table :data="tbWmsMaterialReceiptFormData.itemList" border stripe fit highlight-current-row>
          <el-table-column align="center" label="序" width="95">
            <template slot-scope="scope">
              <span>{{ scope.$index + 1 }}</span>
            </template>
          </el-table-column>
          <el-table-column label="入库物料编码" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.materialNo }}</span>
            </template>
          </el-table-column>
          <el-table-column label="物料图号" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.skuCode }}</span>
            </template>
          </el-table-column>
       <el-table-column label="入库物料" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.materialName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="数量" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.quantity }}</span>
            </template>
          </el-table-column>
         <el-table-column label="入库结算单位" align="center">
           <template slot-scope="scope">
             <span>{{ scope.row.measUnitNo }}</span>
           </template>
         </el-table-column>
            <el-table-column label="结算基于" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.shipByPackOrqty }}</span>
              </template>
            </el-table-column>
           <el-table-column label="每包装数量" align="center">
             <template slot-scope="scope">
               <span>{{ scope.row.qtyPerPack }}</span>
             </template>
           </el-table-column>
           <el-table-column label="数量" align="center">
             <template slot-scope="scope">
               <span>{{ scope.row.quantity }}</span>
             </template>
           </el-table-column>
           <el-table-column label="包装数量" align="center">
             <template slot-scope="scope">
               <span>{{ scope.row.packQty }}</span>
             </template>
           </el-table-column>
          <el-table-column label="重量" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.weight }}</span>
            </template>
          </el-table-column>
            <el-table-column label="体积" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.volume }}</span>
              </template>
            </el-table-column>
            <el-table-column label="结算费用" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.countCarriage }}</span>
              </template>
            </el-table-column>
            <el-table-column label="货值" align="center">
              <template slot-scope="scope">
                <span>{{ scope.row.cargoValue }}</span>
              </template>
            </el-table-column>
          <el-table-column label="单位" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.weightMeasUnit }}</span>
            </template>
          </el-table-column>
           <el-table-column label="单位" align="center">
             <template slot-scope="scope">
               <span>{{ scope.row.packing }}</span>
             </template>
           </el-table-column>
          <el-table-column label="单位" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.unitPrice }}</span>
            </template>
          </el-table-column>
           <el-table-column label="单位体积" align="center">
             <template slot-scope="scope">
               <span>{{ scope.row.unitVolume }}</span>
             </template>
           </el-table-column>
          <el-table-column label="单位重量" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.unitWeight }}</span>
            </template>
          </el-table-column>
          <el-table-column label="单价" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.countPrice }}</span>
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width">
            <template slot-scope="scope">

              <el-button type="text" @click="handleItemDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-collapse-item>
    </el-collapse>
  </div>
</template>

<script>
  import appConstant from '@/constant/AppConstant'
  import TbWmsMaterialReceiptApi from '@/api/web/TbWmsMaterialReceiptApi'
  import WarehouseCustomerSelector from '@/views/pms/tWhWarehouseCustomerMaterial/WarehouseCustomerSelector'
  // import PurchaseOrderSelector from '@/views/order/purchaseOrder/PurchaseOrderSelector'
/* import WarehouseSelector from '@/views/web/tWhWarehouse/WarehouseCustomerSelector' */
import TreeUtils from '@/utils/TreeUtils'
import LocationApi from '@/api/basic/LocationApi'
import TWhWarehouseCustomerApi from '@/api/pms/TWhWarehouseCustomerApi'
import CustomerSelector from '@/views/basic/customer/CustomerSelector'
import SalesOrderSelector from '@/views/order/salesOrder/SalesOrderSelector'
  export default {
   name: 'TbWmsMaterialReceiptImport',
   components: {
   //  PurchaseOrderSelector
    CustomerSelector,
    WarehouseCustomerSelector,
    SalesOrderSelector
    /* WarehouseSelector */
   },

    filters: {
      /* categoryFilter(item) {
        const values = appConstant.categoryList.filter(value => value.key === item)
        if (values && values.length > 0) {
          return values[0].label
        } else {
          return ''
        }
      } */
    },
    data() {
      return {
        activeNames: [
          'basicInfo',
          'itemInfo'
        ],
        selects: {
          locationList: [],
          customerTypeList: [],
          orderHandlingTypeList: appConstant.orderHandlingTypeList,
          settledBasedOnList: appConstant.settledBasedOnList,
          warehouseList: [],
          orderTypeList: appConstant.orderTypeList
        },
        operation: undefined,


   warehousSelectorVisible: false,
        tbWmsMaterialReceiptFormData: {
          orderNo: null,
            itemList: [],
            warehouseCode: null,

            consignmentLocation: null,
            customerNo: null,
            customerName: null,
            CustomerType: null,
            shipperContact: null,
            shipperAddress: null,
            status: '',
            orderHandlingType: null,
            orderType: null,
            preArrivedTime: null,
            shipperZipNo: null,
            remark: null,
            receiptNo: null,
            orderTime: null,
            configColFive: null,
            configColFour: null,
            configColThree: null,
            configColTwo: null,
            configColOne: null,
            sourceType: null,
            sourceOrderNo: null,
            departureIdList: [],
            shipperPhoneNo: null
          },
          itemDisplay: false,
          itemData: {
            materialName: undefined,
            materialNo: undefined,
            measUnitNo: undefined,
            stageNo: undefined,
            shipByPackOrqty: undefined,
            arrivedQty: 0,
            qtyPerPack: 0,
            quantity: 0,
            status: 0,
            packQty: 0,
            weight: 0,
            volume: 0,
            countCarriage: 0,
            cargoValue: 0,
            weightMeasUnit: undefined,
            packing: undefined,
            unitPrice: 0,
            unitVolume: 0,
            unitWeight: 0,
            skuCode: 0,
            countPrice: 0
          },
          itemRules: {
            materialName: [{ required: true, message: '_', trigger: 'blur' }]
          },
        // 校验规则的名称必须和校验字段的名称一致
        rules: {
          /*  departureIdList: [{ required: true, message: '_', trigger: 'change' }],
            destinationIdList: [{ required: true, message: '_', trigger: 'change' }] */
        },

        contractSelectorVisible: false,
        fromSelectorVisible: false,
        toSelectorVisible: false,
         materialSelectorVisible: false
         /*  fromSelectorVisible: false,
          toSelectorVisible: false,
          orgSelectorVisible: false,
          vehicleModelSelectorVisible: false, */


      }

    },
    created() {

      this.loadSelects()
    },
    methods: {

      loadSelects() {


         LocationApi.list().then(response => {
            this.selects.locationList = TreeUtils.list2Tree(response.data)
          })
      },
      handleLocationChange() {
        // 先清空里程

     /*   if (this.data.departureIdList.length !== 0 && this.data.destinationIdList.length !== 0) {
          // 根据起始地和目的地，查询线路信息
          const params = {
            startLocation: this.data.departureIdList.join(','),
            endLocation: this.data.destinationIdList.join(',')
          }
          TransitRouteApi.list(params).then(response => {
            if (response.data.length > 0) {
              // 设置里程
              this.data.miles = response.data[0].milometer
              this.data.estDeliveryDays = response.data[0].estDeliveryDays
            }
          })
        } */
      },
      handleSettleExpense() {
        if (this.itemData.countQuantity) {

          this.itemData.countCarriage = this.itemData.countQuantity*this.itemData.countPrice

        }
     },
      handleCalShipmentExpense() {

        if (this.itemData.shipByPackOrqty) {
           if (this.itemData.quantity) {
           this.itemData.cargoValue = this.itemData.quantity * this.itemData.unitPrice
           }
          if (this.itemData.shipByPackOrqty === 'weight') {

            if (this.itemData.unitWeight) {

              this.itemData.countQuantity = this.itemData.quantity*this.itemData.unitWeight
              this.itemData.weight  = this.itemData.quantity*this.itemData.unitWeight
             this.itemData.volume  = this.itemData.quantity*this.itemData.unitVolume
            }
          } else if (this.itemData.shipByPackOrqty === 'qty') {
            if (this.itemData.quantity) {
              this.itemData.countQuantity = this.itemData.quantity
            this.itemData.weight  = this.itemData.quantity*this.itemData.unitWeight
             this.itemData.volume  = this.itemData.quantity*this.itemData.unitVolume
            }
          } else if (this.itemData.shipByPackOrqty === 'pack') {
            if (this.itemData.packQty) {
              this.itemData.countQuantity = this.itemData.packQty
             this.itemData.quantity = this.itemData.packQty*this.itemData.qtyPerPack
			 this.itemData.weight  = this.itemData.packQty*this.itemData.unitWeight
			 this.itemData.volume  = this.itemData.packQty*this.itemData.unitVolume
			 // console.log('1'+ this.itemData.quantity)
			 // console.log('2'+this.itemData.packQty)
			 // console.log('3'+this.itemData.qtyPerPack)
            }
          } else {
            if (this.itemData.unitVolume) {
              this.itemData.countQuantity =   this.itemData.quantity*this.itemData.unitVolume
              this.itemData.volume = this.itemData.quantity * this.itemData.unitVolume
			   this.itemData.weight  = this.itemData.quantity*this.itemData.unitWeight
			  console.log('1\n'+this.itemData.volume)
			  console.log('2\n'+this.itemData.quantity)
			  console.log('3\n'+this.itemData.unitVolume)
            }
          }

        } else {
          this.itemData.countQuantity = 0
        }
      },
      handleCalMaterialValue() {
        if (this.itemData.unitPrice) {
          if (this.itemData.weight) {
            this.itemData.cargoValue = this.itemData.unitPrice * this.itemData.weight
          }

          this.handleCalInsuranceExpense()
        } else {
          this.itemData.materialValue = 0
        }
      },
      customerAtSelectChange() {
        if (this.tbWmsMaterialReceiptFormData.warehouseCode) {
          TWhWarehouseCustomerApi.get(this.tbWmsMaterialReceiptFormData.warehouseCode).then(response => {
            if(response.data!=null){
                 this.tbWmsMaterialReceiptFormData.warehouseCode= response.data.id
                  this.tbWmsMaterialReceiptFormData.shipperAddress= response.data.address
                   this.tbWmsMaterialReceiptFormData.shipperPhoneNo= response.data.mobile
                   this.tbWmsMaterialReceiptFormData.shipperContact= response.data.manager
                   this.tbWmsMaterialReceiptFormData.consignmentLocation = response.data.locationCity
                   if (this.tbWmsMaterialReceiptFormData.consignmentLocation) {
                     this.tbWmsMaterialReceiptFormData.departureIdList = this.tbWmsMaterialReceiptFormData.consignmentLocation.split(',')
                   }
              }

            })
          }
       },

      customerAtChange() {
       const params = {
          customerNo: this.tbWmsMaterialReceiptFormData.customerNo
        }
       this.tbWmsMaterialReceiptFormData.warehouseCode= ""
                this.tbWmsMaterialReceiptFormData.shipperAddress= ""
                 this.tbWmsMaterialReceiptFormData.shipperPhoneNo= ""
                 this.tbWmsMaterialReceiptFormData.shipperContact= ""
                 this.tbWmsMaterialReceiptFormData.consignmentLocation = ""
                   this.tbWmsMaterialReceiptFormData.departureIdList = ""

          TWhWarehouseCustomerApi.list(params).then(response => {
            this.selects.warehouseList = response.data
            if(response.data!=null&&response.data.length>0){
               this.tbWmsMaterialReceiptFormData.warehouseCode= response.data[0].id
                this.tbWmsMaterialReceiptFormData.shipperAddress= response.data[0].address
                 this.tbWmsMaterialReceiptFormData.shipperPhoneNo= response.data[0].mobile
                 this.tbWmsMaterialReceiptFormData.shipperContact= response.data[0].manager
                 this.tbWmsMaterialReceiptFormData.consignmentLocation = response.data[0].locationCity
                 if (this.tbWmsMaterialReceiptFormData.consignmentLocation) {
                   this.tbWmsMaterialReceiptFormData.departureIdList = this.tbWmsMaterialReceiptFormData.consignmentLocation.split(',')
                 }
            }

          })
      },

      handleMaterialSelected(obj) {
        console.log(obj)
        this.itemData = {
          ...this.itemData,
          materialName: obj.skuName,
           materialNo: obj.materialNo,
           shipByPackOrqty: obj.shipByPackOrqty,
           qtyPerPack: obj.convertQuantity,
           unitPrice: obj.unitPrice,
           unitVolume: obj.unitVolume,
           skuCode:obj.skuCode,
           measUnitNo: obj.measUnitNo,
           weightMeasUnit: obj.measUnitNo,
           packing: obj.measUnitNo,
           unitWeight: obj.unitWeight

        /*  cargoFreightType: obj.categoryId,
          unitPrice: obj.unitPrice,
          cargoFreightNo: obj.id,
          unitWeight: obj.unitWeight,
          weightMeasUnit: obj.weightMeasUnit */
        }

        this.itemData.materialName =  obj.skuName,
        this.itemData.shipByPackOrqty =  obj.shipByPackOrqty,
         this.itemData.qtyPerPack =  obj.qtyPerPack,
         this.itemData.unitVolume =  obj.unitVolume,
          this.itemData.unitWeight =  obj.unitWeight,
          this.itemData.measUnitNo= obj.measUnitNo,
        this.itemData.unitPrice =  obj.unitPrice
       if(obj.shipByPackOrqty!=null&&obj.shipByPackOrqty!=""){
          if(obj.shipByPackOrqty==="weight"){
            this.itemData.weightMeasUnit= obj.measUnitNo

         }else if(obj.shipByPackOrqty==="vol"){
            this.itemData.measUnitNo= obj.measUnitNo
          }else  if(obj.shipByPackOrqty==="pack"){
            this.itemData.packing= obj.measUnitNo
          } else{
         this.itemData.measUnitNo= obj.measUnitNo
          }
        }
      },
      handleItemFormDisplay() {
        var that = this
        this.itemDisplay = !this.itemDisplay
        this.itemData = {}
     /*   const params = {
          orderType: this.tbWmsMaterialReceiptFormData.orderType,
          orderHandlingType: this.tbWmsMaterialReceiptFormData.orderHandlingType,
          customerNo:this.tbWmsMaterialReceiptFormData.customerNo,
          materialNo:this.itemData.materialNo
        } */
      /*  RoutePriceNewApi.list(params).then(response => {
          if (response.data.length > 0) {
            // 设置单价
            that.itemData.countPrice = response.data[0].basicAmt
            // that.$set(that.itemData, countPrice, response.data[0].basicAmt);
          }
        }) */
      },
      openMaterialSelector() {
        this.$refs.warehouseCustomerSelector.loadData()
        this.materialSelectorVisible = true
      },
      openWarehouseSelector() {
        this.warehousSelectorVisible = true
      },
      handleWarehouseSelected(obj) {
        this.tbWmsMaterialReceiptFormData.warehouseCode = obj[0].id
        this.tbWmsMaterialReceiptFormData.shipperPhoneNo = obj[0].warehousePhone

         this.tbWmsMaterialReceiptFormData.consignmentLocation = obj[0].locationCity
        if (this.tbWmsMaterialReceiptFormData.consignmentLocation) {
          this.tbWmsMaterialReceiptFormData.departureIdList = this.tbWmsMaterialReceiptFormData.consignmentLocation.split(',')
        }
      },

      openFromSelector() {
        this.fromSelectorVisible = true
      },
      handleFromSelected(obj) {
        console.log(obj)

        this.tbWmsMaterialReceiptFormData.customerNo = obj.id
        this.tbWmsMaterialReceiptFormData.customerName = obj.customerName
       this.tbWmsMaterialReceiptFormData.customerPhone = obj.phone
       this.tbWmsMaterialReceiptFormData.customerAddress = obj.address
        this.tbWmsMaterialReceiptFormData.consignmentLocation = obj.areaDesc
        if (this.tbWmsMaterialReceiptFormData.consignmentLocation) {
          this.tbWmsMaterialReceiptFormData.departureIdList = this.data.consignmentLocation.split(',')
        }
      /*  this.$refs.warehouseCustomerSelector.loadData() */

        this.customerAtChange()
      },
      openToSelector() {
        this.toSelectorVisible = true
      },
      handleItemAdd() {
        if (this.itemData.materialName) {
          // 解决itemList中所有item的内存地址一样的问题
          const data = {
            ...this.itemData
          }
          // 校验必填字段
          this.$refs['itemForm'].validate((valid) => {
            if (valid) {
              this.tbWmsMaterialReceiptFormData.itemList.push(data)
              this.itemData = {
              }
              this.$message.success('添加成功！')
            }
          })
        } else {
          this.$message.warning('请选择物料！')
        }
      },
      openContractSelector() {
        this.contractSelectorVisible = true
      },
      handleSelectedContract(purchaseOrders) {
        if (purchaseOrders && purchaseOrders.length > 0) {
          for (let i = 0; i < purchaseOrders.length; i++) {
            const purchaseOrder = purchaseOrders[i]
            this.tbWmsMaterialReceiptFormData.customerNo = purchaseOrder.customerId
            this.tbWmsMaterialReceiptFormData.sourceOrderNo= purchaseOrder.salesOrderNo
            this.tbWmsMaterialReceiptFormData.customerName = purchaseOrder.customerName
            if (purchaseOrder.items && purchaseOrder.items.length > 0) {
              for (let j = 0; j < purchaseOrder.items.length; j++) {
                const purchaseItem = purchaseOrder.items[j]
                this.tbWmsMaterialReceiptFormData.itemList.push({
                  materialNo: purchaseItem.spuId,
                  quantity: purchaseItem.qty
                })
              }
            }
          }
        }
      },
      handleSubmit() {

        // 将数组转为字符串
     if (this.tbWmsMaterialReceiptFormData.departureIdList) {
            this.tbWmsMaterialReceiptFormData.consignmentLocation = this.tbWmsMaterialReceiptFormData.departureIdList.join(',')
          }

        TbWmsMaterialReceiptApi.addReceipt(this.tbWmsMaterialReceiptFormData).then(response => {
          this.$message.success('新增入库计划成功！')
          this.$router.go(-1)
        })


      },
      handleCancel() {
        this.$router.go(-1)
      },
      handleChange(val) {
        console.log(val)
      },




    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
